package com.liangzhong.model.club
{
	import com.liangzhong.constants.PlayerConst;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.club.vo.AchieveDateVo;
	import com.liangzhong.model.club.vo.BuffVo;
	import com.liangzhong.model.club.vo.ClubInformation;
	import com.liangzhong.model.club.vo.Sysini;
	import com.liangzhong.model.club.vo.TeamIconVo;
	import com.liangzhong.model.club.vo.TrophyVo;
	
	import flash.events.Event;

	/**
	 *俱乐部管理器 
	 * @author Administrator
	 * 
	 */	
	public class ClubDataManager
	{
		/**
		 * 俱乐部信息 
		 */		
		private var _clubInfo:ClubInformation = new ClubInformation();
		/**
		 *相关活动 
		 */
		public var sysini:Sysini = new Sysini();
		
		public var achieveDate:AchieveDateVo = new AchieveDateVo();
		/**
		 *球衣数据 
		 */
//		public var _clothArr:Array = [1,2,3,4];
		
		/**
		 *队徽 
		 */
		private var _teamIconVec:Vector.<TeamIconVo>;
		/**
		 *队徽类型 
		 */		
		private var _teamIconType:Array;
		/**
		 *奖杯 
		 */
		public var trophyArr:Array;
		
		/**
		 *buff列表 
		 */
		public var buffList:Array;
		/**
		 *球衣列表 
		 */
		public var trikotList:Array;
		/**
		 * 新手引导步骤
		 */		
		public var guideStep :int;
		/**
		 * 新手引导等级
		 */		
		public var guideLev	  :int;
		
		public function ClubDataManager()
		{
		}
		/**
		 * 获得俱乐部信息 
		 * 
		 */
		public function analysisClubInfo($obj:Object):void
		{
			guideStep = $obj.newerGuide.step;
			guideLev = $obj.newerGuide.level;
			_clubInfo.analysis($obj.clubInfo);
			analysisBuff($obj.buff);
			trikotList = $obj.trikot;
//			trikotList = [];
//			for(var i:int = 1;i< 76;i++)
//			{
//				trikotList.push(i);
//			}
			
			AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.FRIEND_DATA_CHANGE,$obj.invitednum);
			AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.CLUB_INFO);
		}
		/**
		 *解析buff数据 
		 * @param $obj
		 */
		private function analysisBuff($obj:Object):void
		{
			buffList = [];
			var buffVo:BuffVo;
			for each(var obj:Object in $obj)
			{
				buffVo = new BuffVo();
				buffVo.analysis(obj);
				buffList.push(buffVo);
			}
			var vipBuff :BuffVo = new BuffVo();
			vipBuff.buffid 	= _clubInfo.VIP;
			vipBuff.time 	= 0;
			vipBuff.contime = 0;
			buffList.unshift(vipBuff);
			AppEventDispatcher.getInstance().dispatchEvent(new Event(NotifierEvent.BUFFER_CHANFE));
		}
		/**
		 * 俱乐部信息
		 */
		public function get clubInfo():ClubInformation
		{
			return _clubInfo;
		}
		/**
		 *改变经济值
		 * 欧元 
		 * 声望
		 * 点券
		 * 
		 */
		public function changeEconomy($coin:int,$s:int,$cashpoint:int):void
		{
			_clubInfo.changeEconomy($coin,$s,$cashpoint);
			AppEventDispatcher.getInstance().dispatchEvent(new Event(NotifierEvent.BASE_INFO_CHANGE));
		}
		/**
		 *改变buffer 
		 * @param buffVo
		 */		
		public function changeBuff(buffVo :BuffVo,buffPos :int):void
		{
			var buff :BuffVo = buffList[buffPos - 1];
			buff.buffid = buffVo.buffid;
			if(buff.time != buffVo.time)
			{
				buff.contime = 0;
			}
			buff.time = buffVo.time;
			buff.contime += buffVo.contime;
		}
		/**
		 *获取buff通过id 
		 * @param id
		 * @return 
		 * 
		 */		
		public function getBuffById(id :int):BuffVo
		{
			for each(var buff :BuffVo in buffList)
			{
				if(buff.buffid == id)
				{
					return buff;
				}
			}
			return null;
		}
		///////////////////////////////  队徽 //////////////////////////////////////
		/**
		 *获取队徽数据 
		 * @return 
		 * 
		 */		
		public function get teamIconVec():Vector.<TeamIconVo>
		{
			if( _teamIconVec == null)
			{
				analysisTeamIcon();
			}
			return _teamIconVec;
		}
		/**
		 * 获取队徽ID 
		 * @param $type
		 * @param $color
		 * @return 如果为0 则没有此类型的队徽
		 * 
		 */
		public function getTeamIconId($type:String,$color:int):TeamIconVo
		{
			if( _teamIconVec == null)
			{
				analysisTeamIcon();
			}
			var len:int = _teamIconVec.length;
			for(var i:int = 0;i < len;i++)
			{
				if(_teamIconVec[i].type == $type && _teamIconVec[i].color == $color)
					return _teamIconVec[i]/*.iconId;*/
			}
			return null;
		}
		/**
		 *解析队徽数据 
		 * 
		 */
		private function analysisTeamIcon():void
		{
			_teamIconVec = new Vector.<TeamIconVo>();
			_teamIconType = [];
			var temparr:Array = [];
			var xml:XML = ResourceManager.instance.getResource(ResourceConst.CLUB_TEAM_ICON);
			
			var xmlList:XMLList = xml.children();
			var icon:TeamIconVo;
			var len:int = xmlList.length();
			for(var i:int = 0;i < len;i++)
			{
				icon = new TeamIconVo();
				icon.analysisData(xmlList[i]);
				_teamIconVec.push(icon);
				if(temparr.indexOf(icon.type) == -1)
				{
					temparr.push(icon.type);
					_teamIconType.push(icon);
				}
			}
		}
		/**
		 *获得队徽Vo By ID 
		 * @param $iconId
		 * 
		 */
		public function getTeamIconVo($iconId:int):TeamIconVo
		{
			if( _teamIconVec == null)
			{
				analysisTeamIcon();
			}
			var len:int = _teamIconVec.length;
			for(var i:int = 0;i < len;i++)
			{
				if(_teamIconVec[i].iconId == $iconId)
					return _teamIconVec[i];
			}
			return null;
		}
		/**
		 *队 徽类型
		 */
		public function get teamIconType():Array
		{
			if( _teamIconVec == null)
			{
				analysisTeamIcon();
			}
			return _teamIconType;
		}
		
		
//		/**
//		 *球员位置 
//		 * @param $player
//		 * @return 
//		 * 
//		 */
//		public static function playerPlayer($player:int):String
//		{
//			switch($player)
//			{
//				case PlayerConst.PLAYERPLAYER_GK:
//					return "守门员";
//				case PlayerConst.PLAYERPLAYER_DF:
//					return "后卫";
//				case PlayerConst.PLAYERPLAYER_MF:
//					return "中场";
//				case PlayerConst.PLAYERPLAYER_FW:
//					return "前锋";
//				case PlayerConst.PLAYERPLAYER_ALL:
//					return "所有球员";
//			}
//			return "所有球员";
//		}
		/**
		 *解析奖杯 
		 * 
		 */
		public function get trophyData():Array
		{
			if(trophyArr != null)return trophyArr;
			trophyArr = [];
			var xml:XML = ResourceManager.instance.getResource(ResourceConst.CLUB_TROPHY);
			
			var xmlList:XMLList = xml.children();
			var icon:TrophyVo;
			var len:int = xmlList.length();
			for(var i:int = 0;i < len;i++)
			{
				icon = new TrophyVo();
				icon.analysisData(xmlList[i]);
				trophyArr.push(icon);
			}
			return trophyArr;
		}
		
		
		
		/**
		 *解析活动相关 
		 * @param $obj
		 * 
		 */
		public function analysisSysini($obj:Object):void
		{
			sysini.analysis($obj);
		}
		
		/**
		 *解析 成就数据 
		 * @param $obj
		 * 
		 */
		public function analysisAchieve($obj:Object):void
		{
			achieveDate.acalysis($obj);
		}
	}
}